import json
import random

## 以下为三种图表的测试数据
chart0 = {
    "name": "比赛基本信息图",
    "chart_type": "basic_match_light",
    "data": {
        "team1": {
            "name": "森林狼",
            "history": "(赛前战绩 19胜45负)",
            "logo": "http://stat-nba.com/image/teamImage/MIN.gif",
            "score": ["33", "21", "25", "28"],
            "final_score": "107",
            "home": "客场",
            "color": "#08d9d6"
        },
        "team2": {
            "name": "雷霆",
            "history": "(赛前战绩 40胜24负)",
            "logo": "http://stat-nba.com/image/teamImage/OKC.gif",
            "score": ["42", "22", "21", "26"],
            "final_score": "111",
            "home": "主场",
            "color": "#ff2e63"
        }
    },
    "description": {
        "name": "队伍名称",
        "history": "赛前成绩",
        "logo": "队伍图标",
        "score": "各节得分情况",
        "final_score": "最终比分",
        "home": "场次",
        "color": "图表颜色"
    }
}

chart2 = {
    "name": "球队基础数据图",
    "chart_type": "team_match",
    "data": {
        "team1": {
            "name": "森林狼",
            "shoot": "51.3%",
            "tpt": "50.0%",
            "penalty": "81.0%",
            "backboard": "26",
            "assisting": "26",
            "steals": "7",
            "block_shots": "5",
            "miss": "16",
            "foul": "20",
            "color": "#08d9d6"
        },
        "team2": {
            "name": "雷霆",
            "shoot": "51.9%",
            "tpt": "30.8%",
            "penalty": "95.5%",
            "backboard": "42",
            "assisting": "28",
            "steals": "7",
            "block_shots": "8",
            "miss": "18",
            "foul": "24",
            "color": "#ff2e63"
        }
    },
    "description": {
        "name": "队伍名称",
        "shoot": "投篮",
        "tpt": "三分",
        "penalty": "罚球",
        "backboard": "篮板",
        "assisting": "助攻",
        "steals": "抢断",
        "block_shots": "盖帽",
        "miss": "失误",
        "foul": "犯规",
        "color": "图表颜色"
    }
}

chart3 = {
    "name": "球员场均雷达图",
    "chart_type": "avg_player",
    "data": {
        "name_cn": "安德鲁-威金斯",
        "name_eng": "Andrew Wiggins",
        "img": "http://stat-nba.com/image/playerImage/4241.jpg",
        "shoot": "57.1%",
        "backboard": "3",
        "assisting": "1",
        "steals": "1",
        "block_shots": "1",
        "miss": "1",
        "foul": "3",
        "score": "23",
        "shoot_avg": "44.1%",
        "backboard_avg": "4.4",
        "assisting_avg": "2.3",
        "steals_avg": "1.0",
        "block_shots_avg": "0.6",
        "miss_avg": "2.1",
        "foul_avg": "2.2",
        "score_avg": "19.7"
    },
    "description": {
        "name_cn": "球员中文名字",
        "name_eng": "球员英文名字",
        "img": "球员图片",
        "shoot": "投篮",
        "backboard": "篮板",
        "assisting": "助攻",
        "steals": "抢断",
        "block_shots": "盖帽",
        "miss": "失误",
        "foul": "犯规",
        "score": "得分",
        "shoot_avg": "生涯平均投篮",
        "backboard_avg": "生涯平均篮板",
        "assisting_avg": "生涯平均助攻",
        "steals_avg": "生涯平均抢断",
        "block_shots_avg": "生涯平均盖帽",
        "miss_avg": "生涯平均失误",
        "foul_avg": "生涯平均犯规",
        "score_avg": "生涯平均得分"
    }
}

chart4 = {
    "name": "球员基础数据图",
    "chart_type": "basic_player",
    "data": {
        "header": ["球员", "首发", "时间", "投篮%", "命中", "出手", "三分%", "命中", "出手", "罚球%", "命中", "出手", "真实命中%", "篮板", "前场", "后场",
                   "助攻", "抢断", "盖帽", "失误", "犯规", "得分"],
        "team1": {
            "name": "森林狼",
            "value": [
                ["安德鲁-威金斯", "1", "40", "57.1%", "8", "14", "50.0%", "4", "8", "50.0%", "3", "6", "69.1%", "3", "0", "3",
                 "1", "1", "1", "1", "3", "23"],
                ["卡尔-安东尼-唐斯", "1", "35", "52.9%", "9", "17", "33.3%", "1", "3", "100.0%", "4", "4", "61.3%", "9", "3",
                 "6", "0", "2", "1", "2", "5", "23"],
                ["吉米-巴特勒", "1", "41", "43.8%", "7", "16", "33.3%", "1", "3", "100.0%", "7", "7", "57.7%", "3", "1", "2",
                 "7", "3", "2", "3", "2", "22"],
                ["杰夫-蒂格", "1", "34", "80.0%", "4", "5", "66.7%", "2", "3", "50.0%", "1", "2", "93.5%", "3", "0", "3",
                 "10", "0", "0", "2", "1", "11"],
                ["泰-吉布森", "1", "36", "50.0%", "3", "6", "100.0%", "1", "1", "100.0%", "2", "2", "65.4%", "4", "0", "4",
                 "1", "1", "0", "4", "4", "9"],
                ["戈尔吉-吉昂", "0", "22", "83.3%", "5", "6", "0", "0", "0", "0", "0", "0", "83.3%", "3", "2", "1", "2", "0",
                 "0", "2", "2", "10"],
                ["贾马尔-克劳福德", "0", "18", "33.3%", "3", "9", "50.0%", "3", "6", "0", "0", "0", "50.0%", "1", "0", "1",
                 "3", "0", "1", "1", "2", "9"],
                ["泰尔斯-琼斯", "0", "11", "0.0%", "0", "2", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "2", "0", "0",
                 "1", "1", "0"],
                ["沙巴兹-穆罕默德", "0", "3", "0.0%", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
                 "0", "0", "0", "0"]
            ]
        },
        "team2": {
            "name": "雷霆",
            "value": [
                ["保罗-乔治", "1", "42", "47.6%", "10", "21", "55.6%", "5", "9", "100.0%", "11", "11", "69.7%", "4", "0",
                 "4", "9", "0", "3", "2", "4", "36"],
                ["史蒂文-亚当斯", "1", "32", "100.0%", "11", "11", "0.0%", "0", "0", "100.0%", "5", "5", "102.3%", "6", "4",
                 "2", "0", "1", "0", "0", "5", "27"],
                ["拉塞尔-威斯布鲁克", "1", "37", "28.6%", "6", "21", "0.0%", "0", "8", "75.0%", "3", "4", "33.0%", "9", "4",
                 "5", "14", "2", "0", "7", "2", "15"],
                ["卡梅罗-安东尼", "1", "29", "57.1%", "4", "7", "33.3%", "1", "3", "0.0%", "0", "0", "64.3%", "6", "0", "6",
                 "1", "0", "0", "1", "3", "9"],
                ["安德烈-罗伯森", "1", "32", "60.0%", "3", "5", "0.0%", "0", "2", "0.0%", "0", "0", "60.0%", "6", "3", "3",
                 "0", "2", "2", "4", "5", "6"],
                ["杰拉米-格兰特", "0", "25", "8", "0.0%", "4", "5", "100.0%", "11", "0.0%", "0", "0", "90.0%", "3", "1", "2",
                 "1", "1", "2", "0", "1", "9"],
                ["雷蒙德-费尔顿", "0", "11", "50.0%", "2", "4", "0.0%", "0", "1", "0.0%", "0", "0", "50.0%", "1", "0", "1",
                 "1", "0", "0", "2", "0", "4"],
                ["约什-胡斯蒂斯", "0", "13", "25.0%", "1", "4", "50.0%", "1", "2", "0.0%", "0", "0", "37.5%", "1", "1", "0",
                 "0", "0", "0", "0", "2", "3"],
                ["尼克-科里森", "0", "2", "0.0%", "0", "0", "0.0%", "0", "0", "100.0%", "2", "2", "113.6%", "1", "0", "1",
                 "1", "0", "0", "1", "1", "2"],
                ["特伦斯-弗格森", "0", "3", "0.0%", "0", "1", "0.0%", "0", "0", "0.0%", "0", "0", "0.0%", "0", "0", "0", "0",
                 "0", "0", "0", "0", "0"],
                ["帕特里克-帕特森", "0", "14", "0.0%", "0", "0.0%", "0", "0", "0.0%", "0", "0", "0.0%", "5", "0", "5", "1",
                 "1", "0", "1", "1", "0"]
            ]
        }
    },
    "description": {
        "header": "表头信息",
        "name": "队伍名称",
        "value": "对应表头的数据"
    }
}


def makeTextbyChartType(chart):
    chart_text = ''
    chart_type = chart['chart_type']
    data = chart['data']
    if chart_type == 'basic_match_light':
        prefix = "本场比赛最终比分，"
        body = data['team1']['name'] + data['team1']['final_score'] + "分，" + data['team2'][
            'name'] + data['team2']['final_score'] + "分。"
        chart_text = prefix + body
    elif chart_type == 'team_match':
        prefix = "球队基础数据方面，"
        body = data['team1']['name'] + "队投篮命中率" + data['team1']['shoot'] + ", 篮板球" + data['team1'][
            'backboard'] + "个，" + "助攻" + data['team1']['assisting'] + "个，" + "犯规" + data['team1']['foul'] + "次，" + \
               data['team2']['name'] + "队投篮命中率" + data['team2']['shoot'] + ", 篮板球" + data['team2'][
                   'backboard'] + "个，" + "助攻" + data['team2']['assisting'] + "个，" + "犯规" + data['team2']['foul'] + "次。"
        chart_text = prefix + body
    elif chart_type == 'avg_player':
        prefix = "球员个人数据方面" + data['name_cn'] + "拿下"
        body = ''
        postfix_good = ['超常发挥。', '表现亮眼。', '充分展现能力。', '数据可圈可点。', '闪耀全场。', '打出个人价值。', '远超赛季平均水准。']
        postfix_normal = ['正常发挥。', '打出了赛季平均水准。', '表现不温不火。', '展现了应有实力。', '拿下个人平均数据。']
        postfix_bad = ['表现平平。', '没有发挥全部实力。', '有失水准。', '数据有待提升。', '发挥失常。', '不在状态。']
        if int(data['score']) > 5:
            body = body + data['score'] + "分"
        if int(data['backboard']) > 2:
            body = body + data['backboard'] + "篮板"
        if int(data['assisting']) > 2:
            body = body + data['assisting'] + "助攻"
        if int(data['steals']) > 0:
            body = body + data['steals'] + "抢断"
        if int(data['block_shots']) > 0:
            body = body + data['block_shots'] + "盖帽，"
        behaviour = [(float(data['score']) - float(data['score_avg'])),
                     (float(data['backboard']) - float(data['backboard_avg'])),
                     (float(data['assisting']) - float(data['assisting_avg'])),
                     (float(data['steals']) - float(data['steals_avg'])),
                     (float(data['block_shots']) - float(data['block_shots_avg'])),
                     (float(data['shoot'].strip('%')) - float(data['shoot_avg'].strip('%')))]
        count = 0
        for i in behaviour:
            if i > 0:
                count = count + 1
        if count > 4:
            postfix = random.choice(postfix_good)
        elif count < 2:
            postfix = random.choice(postfix_bad)
        else:
            postfix = random.choice(postfix_normal)
        chart_text = prefix + body + postfix
    elif chart_type == 'basic_player':
        chart_text = "本场比赛双方出场球员基础数据如表所示。"
    elif chart_type == 'score_rose':
        max_ = max(zip(data.keys(), data.values()))
        max_choice = '三分球'
        if max_[0] == 'second_points':
            max_choice = '两分球'
        elif max_[0] == 'penalty_shot':
            max_choice = '罚球'
        prefix = "球员得分分布方面，"
        body = chart['name'] + "拿下" + str(sum(data.values())) + "分，其中" + max_choice + '占比' + str(round(
            max_[1] / sum(data.values()), 2))
        postfix = ['，成为其主要得分手段。', '，是其第一进攻选择。', '，是他的主要进攻方式。']
        chart_text = prefix + body + random.choice(postfix)
    elif chart_type == 'player_data_parallel':
        players = ''
        for player in data:
            players = players + player['name'] + '，'
        chart_text = "本场比赛所选择的出场球员，" + players + "其基础数据的平行对比，如图所示。"
    elif chart_type == 'player_data_stack':
        players = ''
        for player in data:
            players = players + player['name'] + '，'
        chart_text = "本场比赛所选择的出场球员，" + players + "其基础数据的柱状堆叠对比，如图所示。"
    elif chart_type == 'player_data_polar':
        players = ''
        for player in data:
            players = players + player['name'] + '，'
        chart_text = "本场比赛所选择的出场球员，" + players + "其基础数据的圆环堆叠对比，如图所示。"
    else:
        pass
    return chart_text


if __name__ == '__main__':
    text = makeTextbyChartType(chart3)
    print(text)
